<?php

namespace App\Models;

use App\Services\BaseService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use App\Models\Users;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Translation\Dumper\YamlFileDumper;


class ElectricPaymentOrder extends Model
{
    protected $connection = 'mysql_center';
    protected $table = 'electric_payment_order';
    protected $guarded = [];
//    protected $fillable = [
//        'user_id', 'feeType', 'type', 'paymentMoney', 'pay_type',
//        'orderNo', 'fatherId', 'year', 'month', 'day', 'split_money', 'handlingFee', 'after_handling', 'after_split', 'device_type', 'openid', 'split_type'
//    ];
    public $timestamps = false;

    /**
     * @return HasOne
     */
    public function user_info(): HasOne
    {
        return $this->hasOne(Users::class, "id", "user_id");
    }
    public function platform_agent()
    {
        return $this->hasOne(ElectricPlatformAgent::class, "id", "fatherId");
    }
    public function platform_owner()
    {
        return $this->hasOne(ElectricPlatformOwner::class, "id", "user_id");
    }
    public function getOrderCount($where)
    {
        $count = $this->where($where)->count();
        return $count;
    }

    public function getOrderFieldsSum($where, $fields)
    {
        $total = $this->where($where)->sum($fields);
        return $total;
    }

    public function setWhere($where)
    {

    }

    /**查询订单列表
     * @param string $fields
     * @param string $where
     * @param $wt
     * @param string $sort
     * @param string $sort_str
     * @return mixed
     */
    public static function getOrdersByWhere($fields = '*', $where = '', $wt, string $sort = 'desc', string $sort_str = 'create_time')
    {
        if ($fields == "*") {
            $db = self::select("*");
        } else {
            $db = self::select(Db::raw($fields));
        }

        $where != '' && $db->where($where);
        $wt == 1 && $db->whereDate('create_time', date("Y-m-d", strtotime("-1")));
        $data = $db->orderBy($sort_str, $sort)->get();
        if ($data->count() > 0) {
            $data = $data->toArray();
        } else {
            $data = [];
        }
        return $data;
    }

    /**
     * 获取已支付月份
     * @param $user
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getPaidMonths($user)
    {
        $fields = 'id,paymentMoney,pay_type,feeType,months';
        $where = ['feeType' => 3, 'user_id' => $user['id'], 'status' => 2];
        $order = self::getOrdersByWhere($fields, $where, 2);
        if (!$order) {
            return [];
        }
        $arr = [];
        foreach ($order as $k => $v) {
            foreach (explode(',', $v['months']) as $k1 => $v1) {
                $arr[] = $v1;
            }
        }
        $arr1 = array_unique($arr);//已缴纳月份
        sort($arr1);
        return $arr1;
    }

    /**
     * 获取单一订单
     * @param $where
     * @param string $fields
     * @param int $wt
     * @param string $sort
     * @param string $sort_str
     */
    public static function getOneOrderByWhere($where, $fields = '*', $wt = 1, $sort = 'desc', $sort_str = 'create_time')
    {
        $one = [];
        $db = ElectricPaymentOrder::select(Db::raw($fields));
        $where != '' && $db->where($where);
//        $wt == 1 && $db->whereYear('create_time', date('Y'));
        $order_db = $db->orderBy($sort_str, $sort)->first();
        if ($order_db) {
            $one = $order_db->toArray();
        }
        return $one;
    }
//    public function getFeeTypeAttribute($value)
//    {
//        $arr = [
//            1 => '电费',
//            2 => '水费',
//            3 => '物业费',
//        ];
//        return $arr[$value];
//    }
//    public function getPayTypeAttribute($value)
//    {
//        $arr = [
//            1 => '微信公众号支付',
//            2 => '微信小程序支付',
//            3 => '支付宝支付',
//        ];
//        return $arr[$value];
//    }
}
